<?php

/**
 * 
 * 注册的表单
 * 
 */
class LoginForm extends CFormModel {

    public $UserName;  //用户名
    public $Password;  //密码
    public $VerifyCode; //验证码


    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            // username and password are required
            array('UserName,Password,VerifyCode', 'required'),
            array('VerifyCode', 'checkVerify'),
            array('UserName', 'checkLogin'),
        );
    }
    public function checkVerify(){
        if(!yii::app()->createAction('captcha')->validate($this->VerifyCode, false)){
            $this->addError('VerifyCode', '验证码不正确');
            return false;
        }        
    }

    public function checkLogin(){
         if(!yii::app()->user->login($this->UserName, $this->Password)){
            $this->addError('UserName',yii::app()->user->getError());
            return false;
         }
         return true;   
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'UserName' => '用户名',
            'Password' => '密&#12288;码',
        );
    }
}
